home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / solib.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  14KB  |  454 lines

  1. /* Copyright (C) 1990 Free Software Foundation, Inc.
  2.  
  3. This file is part of GDB.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20. **    symbol definitions
  21. */
  22. #include <sys/types.h>
  23. #include <string.h>
  24. #include <link.h>
  25. #include <sys/param.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include "defs.h"
  29. #include "param.h"
  30. #include "symtab.h"
  31. #include "gdbcore.h"
  32. #include "command.h"
  33. #include "target.h"
  34. #include "frame.h"
  35. #include "regex.h"
  36. #include "inferior.h"
  37.  
  38. extern char *getenv();
  39.  
  40. /*
  41. **    local data declarations
  42. */
  43. #define MAX_PATH_SIZE 256
  44. struct so_list {
  45.     struct link_map inferior_lm;        /* inferior link map */
  46.     struct link_map *inferior_lm_add;
  47.     long   ld_text;            /* FIXME: eliminate, handle arb. sect */
  48.     char inferior_so_name[MAX_PATH_SIZE];    /* Shared Object Library Name */
  49.     struct so_list *next;            /* Next Structure */
  50.     char    symbols_loaded;            /* Flag: loaded? */
  51.     char    from_tty;            /* Flag: print msgs? */
  52.     bfd *so_bfd;
  53.     struct section_table *sections;
  54.     struct section_table *sections_end;
  55. };
  56.  
  57. struct so_list *so_list_head = 0;
  58.  
  59. /*
  60. ** Build a section map for a shared library, record its text size in
  61. ** the so_list structure and set up the text section of the shared lib.
  62. */
  63. static void
  64. solib_map_sections(so)
  65. struct so_list *so;
  66. {
  67.   char *filename;
  68.   char *scratch_pathname;
  69.   int scratch_chan;
  70.   struct section_table *p;
  71.   
  72.   filename = tilde_expand (so->inferior_so_name);
  73.   make_cleanup (free, filename);
  74.   
  75.   scratch_chan = openp (getenv ("PATH"), 1, filename, O_RDONLY, 0,
  76.                 &scratch_pathname);
  77.   if (scratch_chan < 0)
  78.     scratch_chan = openp (getenv ("LD_LIBRARY_PATH"), 1, filename, O_RDONLY, 0,
  79.                 &scratch_pathname);
  80.   if (scratch_chan < 0)
  81.     perror_with_name (filename);
  82.  
  83.   so->so_bfd = bfd_fdopenr (scratch_pathname, NULL, scratch_chan);
  84.   if (!so->so_bfd)
  85.     error ("Could not open `%s' as an executable file: %s",
  86.        scratch_pathname, bfd_errmsg (bfd_error));
  87.   if (!bfd_check_format (so->so_bfd, bfd_object))
  88.     error ("\"%s\": not in executable format: %s.",
  89.        scratch_pathname, bfd_errmsg (bfd_error));
  90.   if (build_section_table (so->so_bfd, &so->sections, &so->sections_end))
  91.     error ("Can't find the file sections in `%s': %s", 
  92.        exec_bfd->filename, bfd_errmsg (bfd_error));
  93.  
  94.   for (p = so->sections; p < so->sections_end; p++)
  95.     {
  96.       p->addr += (CORE_ADDR)so->inferior_lm.lm_addr;
  97.       p->endaddr += (CORE_ADDR)so->inferior_lm.lm_addr;
  98.       if (strcmp (bfd_section_name (so->so_bfd, p->sec_ptr), ".text") == 0)
  99.     {
  100.       /* Determine length of text section */
  101.       so->ld_text = p->endaddr - p->addr;
  102.     }
  103.     }
  104. }
  105.  
  106. /*=======================================================================*/
  107.  
  108. /*    find_solib
  109. **
  110. **Description:
  111. **
  112. **    This module contains the routine which finds the names of any loaded
  113. **    "images" in the current process. The argument in must be NULL on the
  114. **    first call, and then the returned value must be passed in on
  115. **    subsequent calls. This provides the capability to "step" down the
  116. **    list of loaded objects. On the last object, a NULL value is returned.
  117. **    The arg and return value are "struct link_map" pointers, as defined
  118. **    in <link.h>.
  119. **
  120. **    NOTE: This only works under SunOS4.0.
  121. */
  122.  
  123. struct so_list *find_solib(so_list_ptr)
  124. struct so_list *so_list_ptr;            /* so_list_head position ptr */
  125. {
  126. struct so_list *so_list_next = 0;
  127. struct link_map *inferior_lm = 0;
  128. struct link_dynamic inferior_dynamic_cpy;
  129. struct link_dynamic_2 inferior_ld_2_cpy;
  130. struct so_list *new;
  131. int i;
  132.  
  133.      if (!so_list_ptr) {
  134.      if (!(so_list_next = so_list_head)) {
  135.          i = lookup_misc_func ("_DYNAMIC");
  136.          if (i >= 0 && misc_function_vector[i].address != 0) {
  137.          read_memory(misc_function_vector[i].address,
  138.                  &inferior_dynamic_cpy,
  139.                  sizeof(struct link_dynamic));
  140.          if (inferior_dynamic_cpy.ld_version >= 2) {
  141.              read_memory((CORE_ADDR)inferior_dynamic_cpy.ld_un.ld_2,
  142.                  &inferior_ld_2_cpy,
  143.                  sizeof(struct link_dynamic_2));
  144.              inferior_lm = inferior_ld_2_cpy.ld_loaded;
  145.          }
  146.          }
  147.      }
  148.      } else {
  149.      /*
  150.      ** Advance to next local abbreviated load_map structure
  151.      */
  152.      if (!(inferior_lm = so_list_ptr->inferior_lm.lm_next)) {
  153.          /* See if any were added, but be quiet if we can't read
  154.         from the target any more.  */
  155.          int status;
  156.  
  157.          status = target_read_memory (
  158.             (CORE_ADDR)so_list_ptr->inferior_lm_add,
  159.             (char *)&so_list_ptr->inferior_lm,
  160.             sizeof(struct link_map));
  161.          if (status == 0)
  162.            inferior_lm = so_list_ptr->inferior_lm.lm_next;
  163.          else
  164.            inferior_lm = 0;
  165.      }
  166.      so_list_next = so_list_ptr->next;
  167.      }
  168.      if ((!so_list_next) && inferior_lm) {
  169.      /* 
  170.      ** Get Next LM Structure from inferior image and build
  171.      ** an local abbreviated load_map structure
  172.      */
  173.      new = (struct so_list *) xmalloc(sizeof(struct so_list));
  174.          new->inferior_lm_add = inferior_lm;
  175.      read_memory((CORE_ADDR)inferior_lm,
  176.              &new->inferior_lm,
  177.              sizeof(struct link_map));
  178.  
  179.      read_memory((CORE_ADDR)new->inferior_lm.lm_name,
  180.              new->inferior_so_name,
  181.              MAX_PATH_SIZE - 1);
  182.      new->inferior_so_name[MAX_PATH_SIZE - 1] = 0;
  183.      /* Zero everything after the first terminating null */
  184.      strncpy(new->inferior_so_name, new->inferior_so_name, MAX_PATH_SIZE);
  185.  
  186. #if 0
  187.      /* This doesn't work for core files, so instead get ld_text
  188.         using solib_map_sections (below).  */
  189.      read_memory((CORE_ADDR)new->inferior_lm.lm_ld,
  190.              &inferior_dynamic_cpy,
  191.              sizeof(struct link_dynamic));
  192.      read_memory((CORE_ADDR)inferior_dynamic_cpy.ld_un.ld_2,
  193.              &inferior_ld_2_cpy,
  194.              sizeof(struct link_dynamic_2));
  195.      new->ld_text = inferior_ld_2_cpy.ld_text;
  196. #endif
  197.  
  198.      new->next = 0;
  199.      new->symbols_loaded = 0;
  200.      new->so_bfd = NULL;
  201.      new->sections = NULL;
  202.      if (so_list_ptr)
  203.          so_list_ptr->next = new;
  204.      else
  205.          so_list_head = new;
  206.  
  207.      solib_map_sections (new);
  208.  
  209.      so_list_next = new;
  210.      }
  211.      return(so_list_next);
  212. }
  213.  
  214. /* A small stub to get us past the arg-passing pinhole of catch_errors.  */
  215.  
  216. static int
  217. symbol_add_stub (arg)
  218.      char *arg;
  219. {
  220.   register struct so_list *so = (struct so_list *)arg;    /* catch_errs bogon */
  221.  
  222.   symbol_file_add (so->inferior_so_name, so->from_tty,
  223.            (unsigned int)so->inferior_lm.lm_addr, 0);
  224.   return 1;
  225. }
  226.  
  227. /* The real work of adding a shared library file to the symtab and
  228.    the section list.  */
  229.  
  230. void
  231. solib_add (arg_string, from_tty, target)
  232.      char *arg_string;
  233.      int from_tty;
  234.      struct target_ops *target;
  235. {    
  236.   register struct so_list *so = 0;       /* link map state variable */
  237.   char *val;
  238.   int count, old;
  239.   struct section_table *sec;
  240.  
  241.   if (arg_string == 0)
  242.       re_comp (".");
  243.   else if (val = (char *) re_comp (arg_string)) {
  244.       error ("Invalid regexp: %s", val);
  245.   }
  246.  
  247.   /* Getting new symbols may change our opinion about what is
  248.      frameless.  */
  249.   reinit_frame_cache ();
  250.  
  251.   while (so = find_solib(so)) {
  252.       if (re_exec(so->inferior_so_name)) {
  253.       if (so->symbols_loaded) {
  254.           if (from_tty)
  255.             printf("Symbols already loaded for %s\n", so->inferior_so_name);
  256.       } else {
  257.           so->symbols_loaded = 1;
  258.           so->from_tty = from_tty;
  259.                  catch_errors (symbol_add_stub, (char *)so,
  260.             "Error while reading shared library symbols:\n");
  261.       }
  262.       }
  263.   }
  264.  
  265.   /* Now add the shared library sections to the section table of the
  266.      specified target, if any.  */
  267.   if (target) {
  268.     /* Count how many new section_table entries there are.  */
  269.     so = 0;
  270.     count = 0;
  271.     while (0 != (so = find_solib (so))) {
  272.       count += so->sections_end - so->sections;
  273.     }
  274.  
  275.     if (count) {
  276.       /* Reallocate the target's section table including the new size.  */
  277.       if (target->sections) {
  278.     old = target->sections_end - target->sections;
  279.     target->sections = (struct section_table *)
  280.         realloc ((char *)target->sections,
  281.           (sizeof (struct section_table)) * (count + old));
  282.       } else {
  283.     old = 0;
  284.     target->sections = (struct section_table *)
  285.         malloc ((sizeof (struct section_table)) * count);
  286.       }
  287.       target->sections_end = target->sections + (count + old);
  288.  
  289.       /* Add these section table entries to the target's table.  */
  290.       while (0 != (so = find_solib (so))) {
  291.     count = so->sections_end - so->sections;
  292.     bcopy (so->sections, (char *)(target->sections + old), 
  293.            (sizeof (struct section_table)) * count);
  294.     old += count;
  295.       }
  296.     }
  297.   }
  298. }
  299.  
  300. /*=======================================================================*/
  301.  
  302. static void
  303. solib_info()
  304. {
  305. register struct so_list *so = 0;      /* link map state variable */
  306.  
  307.     while (so = find_solib(so)) {
  308.     if (so == so_list_head) {
  309.         printf("      Address Range     Syms Read    Shared Object Library\n");
  310.     }
  311.     printf(" %s - ", local_hex_string_custom (so->inferior_lm.lm_addr, "08"));
  312.     printf("%s   %s   %s\n", 
  313.         local_hex_string_custom (so->inferior_lm.lm_addr + so->ld_text - 1, "08"),
  314.         (so->symbols_loaded ? "Yes" : "No "),
  315.         so->inferior_so_name);
  316.     }
  317.     if (!so_list_head) {
  318.     printf("No shared libraries loaded at this time.\n");    
  319.     }
  320. }
  321.  
  322. /*
  323. ** Called by Insert Breakpoint to see if Address is Shared Library Address 
  324. */
  325. int
  326. solib_address(address)
  327.      CORE_ADDR address;
  328. {
  329. register struct so_list *so = 0;       /* link map state variable */
  330.  
  331.     while (so = find_solib(so)) {
  332.     if ((address >= (CORE_ADDR) so->inferior_lm.lm_addr) &&
  333.         (address < (CORE_ADDR) so->inferior_lm.lm_addr + so->ld_text))
  334.       return 1;
  335.     }
  336.     return 0;
  337. }
  338.  
  339. /*
  340. ** Called by free_all_symtabs
  341. */
  342. void 
  343. clear_solib()
  344. {
  345. struct so_list *next;
  346.  
  347.   while (so_list_head) {
  348.     if (so_list_head->sections)
  349.       free (so_list_head->sections);
  350.     if (so_list_head->so_bfd)
  351.       bfd_close (so_list_head->so_bfd);
  352.     next = so_list_head->next;
  353.     free(so_list_head);
  354.     so_list_head = next;
  355.   }
  356. }
  357.  
  358. /* Called by child_create_inferior when the inferior is stopped at its
  359.    first instruction.  */
  360.  
  361. void 
  362. solib_create_inferior_hook()
  363. {
  364.   struct link_dynamic inferior_dynamic_cpy;
  365.   CORE_ADDR inferior_debug_addr;
  366.   struct ld_debug inferior_debug_cpy;
  367.   int in_debugger;
  368.   CORE_ADDR in_debugger_addr;
  369.   CORE_ADDR breakpoint_addr;
  370.   int i, j;
  371.  
  372.   /* FIXME:  We should look around in the executable code to find _DYNAMIC,
  373.      if it isn't in the symbol table.  It's not that hard to find... 
  374.      Then we can debug stripped executables using shared library symbols.  */
  375.   i = lookup_misc_func ("_DYNAMIC");
  376.   if (i < 0)            /* Can't find shared lib ptr. */
  377.     return;
  378.   if (misc_function_vector[i].address == 0)    /* statically linked program */
  379.     return;
  380.  
  381.   /* Get link_dynamic structure */
  382.   j = target_read_memory(misc_function_vector[i].address,
  383.           (char *)&inferior_dynamic_cpy,
  384.           sizeof(struct link_dynamic));
  385.   if (j)                    /* unreadable */
  386.     return;
  387.  
  388.   /* Calc address of debugger interface structure */
  389.   inferior_debug_addr = (CORE_ADDR)inferior_dynamic_cpy.ldd;
  390.   /* Calc address of `in_debugger' member of debugger interface structure */
  391.   in_debugger_addr = inferior_debug_addr + (CORE_ADDR)((char *)&inferior_debug_cpy.ldd_in_debugger - (char *)&inferior_debug_cpy);
  392.   /* Write a value of 1 to this member.  */
  393.   in_debugger = 1;
  394.   write_memory(in_debugger_addr, &in_debugger, sizeof(in_debugger));
  395.  
  396.   /* Now run the target.  Seeing `in_debugger' set, it will set a
  397.      breakpoint at some convenient place, remember the original contents
  398.      of that place, and eventually take a SIGTRAP when it runs into the
  399.      breakpoint.  We handle this by restoring the contents of the
  400.      breakpointed location (which is only known after it stops), 
  401.      chasing around to locate the shared libraries that have been
  402.      loaded, then resuming.  */
  403.  
  404.   clear_proceed_status ();
  405.   stop_soon_quietly = 1;
  406.   target_resume (0, 0);
  407.   wait_for_inferior ();
  408.   while (stop_signal != SIGTRAP)
  409.     {
  410.       /* FIXME, what if child has exit()ed?  Must exit loop somehow */
  411.       target_resume (0, stop_signal);
  412.       wait_for_inferior ();
  413.     }
  414.   stop_soon_quietly = 0;
  415.  
  416.   /* Set `in_debugger' to zero now.  WHY, is this needed?  */
  417.   in_debugger = 0;
  418.   write_memory(in_debugger_addr, &in_debugger, sizeof(in_debugger));
  419.   read_memory(inferior_debug_addr, &inferior_debug_cpy, sizeof(inferior_debug_cpy));
  420.   /* FIXME: maybe we should add the common symbols from the ldd_cp chain
  421.    * to the misc_function_vector ?
  422.    */
  423.   breakpoint_addr = (CORE_ADDR)inferior_debug_cpy.ldd_bp_addr;
  424.   if (stop_pc - DECR_PC_AFTER_BREAK == breakpoint_addr)
  425.     {
  426.       write_memory(breakpoint_addr, &inferior_debug_cpy.ldd_bp_inst, sizeof(inferior_debug_cpy.ldd_bp_inst));
  427.       if (DECR_PC_AFTER_BREAK)
  428.         {
  429.           stop_pc -= DECR_PC_AFTER_BREAK;
  430.           write_register (PC_REGNUM, stop_pc);
  431.         }
  432.     }
  433.   solib_add ((char *)0, 0, (struct target_ops *)0);
  434. }
  435.  
  436. void
  437. sharedlibrary_command (args, from_tty)
  438.   char *args;
  439.   int from_tty;
  440. {
  441.   dont_repeat();
  442.   solib_add (args, from_tty, (struct target_ops *)0);
  443. }
  444.  
  445. void
  446. _initialize_solib()
  447. {
  448.  
  449.   add_com("sharedlibrary", class_files, sharedlibrary_command,
  450.        "Load shared object library symbols for files matching REGEXP.");
  451.   add_info("sharedlibrary", solib_info, 
  452.        "Status of loaded shared object libraries");
  453. }
  454.